from gpt4all import GPT4All
model_path = r"C:\Users\33720\.cache\gpt4all\Meta-Llama-3-8B-Instruct.Q4_0.gguf"
# 加载模型
model = GPT4All(model_path)

# 每次读取的行数
batch_size = 6

# 读取 txt 文件中的所有单词
with open("temp.txt", "r", encoding="utf-8") as f:
    words = [line.strip() for line in f if line.strip()]

# 分批处理
for i in range(0, len(words), batch_size):
    batch_words = words[i:i + batch_size]

    # 构造提示（prompt）
    prompt = "请根据以下日语单词为每个单词造一个例句，并备注假名和中文翻译。例如：\n"
    prompt += "\n".join(batch_words) + "\n"
    prompt += "格式示例：ハイヒール → High heel → 高跟鞋\n彼女（かのじょ）は赤（あか）いハイヒールを履（は）いている。她穿着红色的高跟鞋。\n"

    # 调用模型生成
    with model.chat_session():
        result = model.generate(prompt, max_tokens=1024)

    # 输出结果或写入文件
    print(result)
    # 或者写入文件
    # with open("output.txt", "a", encoding="utf-8") as out_f:
    #     out_f.write(result + "\n")
